package com.gzsz.service;

import com.gzsz.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import static java.lang.System.*;

public class UsersServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        DBUtil dbUtil=new DBUtil();
        out.println("entry -----");
        String method= request.getParameter("method");
        if (method.equals("delete")){
            String ID= request.getParameter("ID");
            String delete_sql="delete from users where id="+ID+";";
            if (dbUtil.executeUpdateBySQL(delete_sql)>0){
                resp.sendRedirect("/webapps_Web_exploded/users?user_name=&user_type=-1&method=search");
            }else {
                out.println("删除失败");
            }
        } else if (method.equals("search")){
            String user_name = "";//定义一个变量存储用户名字
            if (request.getParameter("user_name")!=null);{
                user_name=request.getParameter("user_name");
            }
            int type=-1;//定义一个变量存储用户权限类型
            if (request.getParameter("user_type")!=null);{
                type=Integer.parseInt(request.getParameter("user_type"));
            }
            String query_sql="select * from users";
            if (user_name == null || user_name.isEmpty() && type<0){
                query_sql="select * from users";
            }
            if (user_name == null || user_name.isEmpty() && type>-1){
                query_sql="select * from users where user_type like '%" +type+ "%'";
            }
            if (user_name != null && type<0){
                query_sql="select * from users where user_name like '%" +user_name+ "%'";
            }
            if (user_name != null && user_name.length()>1 && type>-1){
                query_sql="select * from users where user_type like  "+type+"  and user_name like '%" +user_name+ "%'";
            }
            //得到数据库的结果
            ResultSet resultSet = dbUtil.queryBySQL(query_sql);
            request.setAttribute("user_list",resultSet);//装箱
            //使用请求对象存储这个数据库结果
            request.getRequestDispatcher("html1/users-table.jsp").forward(request,resp);
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        String method= request.getParameter("method");
        DBUtil dbUtil=new DBUtil();
        if (method.equals("add")){
            //1.获取表单的值
            String user_name = request.getParameter("user_name");
            String user_accuont = request.getParameter("user_accuont");
            String user_password = request.getParameter("user_password");
            String user_type = request.getParameter("user_type");
            //2.拼接SQL语句执行数据库
            String add_sql="INSERT INTO Users (user_name, user_accuont, user_password, user_type) " +
                    "VALUES ('"+user_name+"', '"+user_accuont+"', '"+user_password+"', '"+user_type+"');";
            if (dbUtil.executeUpdateBySQL(add_sql)>0){
                resp.sendRedirect("/webapps_Web_exploded/users?user_name=&user_type=-1&method=search");
            }else {
                out.println("添加失败");
            }
        } else if (method.equals("edit")) {

            //1.获取表单的值
            String id = request.getParameter("id");
            String user_name = request.getParameter("user_name");
            String user_accuont = request.getParameter("user_accuont");
            String user_password = request.getParameter("user_password");
            String user_type = request.getParameter("user_type");
            //2.拼接SQL语句执行数据库
            String edit_sql="UPDATE users SET id='"+id+"',user_name='"+user_name+"',user_accuont='"+user_accuont+"',user_password='"+user_password+"',user_type='"+user_type+"' where id='"+id+"'";

            if (dbUtil.executeUpdateBySQL(edit_sql)>0){
//                System.out.println("编辑成功");
                resp.sendRedirect("/webapps_Web_exploded/users?user_name=&user_type=-1&method=search");
            }else {
                out.println("编辑失败");
            }
        } else if (method.equals("login")) {
            //1.获取表单的值
            String user_accuont = request.getParameter("userName");
            String user_password = request.getParameter("Password");
            String user_type = request.getParameter("userType");
            //2.拼接SQL语句执行数据库
            String sql="select * from users where user_accuont='"+user_accuont+"' and  user_password='"+user_password+"' and user_type='"+user_type+"';";
            ResultSet resultSet = dbUtil.queryBySQL(sql);
            try {
                if (resultSet!=null&&resultSet.next()){
//                    System.out.println("成功");
                    resp.sendRedirect("/webapps_Web_exploded/html1/main.jsp");
                }else {
                    resp.sendRedirect("/webapps_Web_exploded/html1/login.jsp");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}




